using System;
using Oracle.ManagedDataAccess.Client;

namespace OracleInternal.BinXml
{
	internal class ObxmlDecodeResponse
	{
		internal object ObxmlDecodeContext;

		internal byte[] OutputBuffer;

		internal long CurrentOffset;

		internal long DecodedTextLength;

		internal ObxmlErrorTypes ErrorType;

		internal string ErrorMessage => GetErrorMessage(ErrorType);

		internal bool IsSuccess => ErrorType == ObxmlErrorTypes.Success;

		internal bool IsDone => ErrorType == ObxmlErrorTypes.Done;

		internal ObxmlDecodeResponse(ObxmlErrorTypes errorType, ObxmlDecodeState parent)
		{
			ResetResponseObject(parent, errorType);
		}

		internal ObxmlDecodeResponse(ObxmlDecodeState parent)
		{
			ResetResponseObject(parent);
		}

		internal void SetSuccess()
		{
			ErrorType = ObxmlErrorTypes.Success;
		}

		internal bool ResetResponseObject(ObxmlDecodeState parent = null, ObxmlErrorTypes errorType = ObxmlErrorTypes.Success)
		{
			ErrorType = errorType;
			OutputBuffer = null;
			CurrentOffset = 0L;
			DecodedTextLength = 0L;
			ObxmlDecodeContext = parent;
			return true;
		}

		internal bool IsResponseValid()
		{
			return OutputBuffer != null;
		}

		internal static string GetErrorMessage(ObxmlErrorTypes errorType, Exception ex = null, ObxmlOpcode.OpcodeIds opcodeId = ObxmlOpcode.OpcodeIds.None)
		{
			string text = "Unknown";
			if (opcodeId != ObxmlOpcode.OpcodeIds.None)
			{
				text = opcodeId.ToString();
			}
			if (ex == null)
			{
				return OracleStringResourceManager.GetErrorMesgWithErrCode(ResourceStringConstants.XML_TYPE_BINARY_INTERNAL_ERROR, errorType.ToString(), text);
			}
			if (errorType == ObxmlErrorTypes.DecodeFailed)
			{
				return OracleStringResourceManager.GetErrorMesgWithErrCode(ResourceStringConstants.XML_TYPE_BINARY_INTERNAL_ERROR, ex.ToString(), text);
			}
			return OracleStringResourceManager.GetErrorMesgWithErrCode(ResourceStringConstants.XML_TYPE_BINARY_INTERNAL_ERROR, errorType.ToString() + ": " + ex.ToString(), text);
		}
	}
}
